<?php 
session_start();
ini_set('max_execution_TIME', 600);
include("dbconfig.php");

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
mysql_select_db($database) or die("Error conecting to db.");



$type=$_GET["type"];
$from_mask=$_GET["from_mask"];
if($type==0)
{
$from_mask=str_replace("-","",$from_mask);
$tb="scanner_".$from_mask;
$res=array();

$query = 'SELECT reg as reg,count(*) as total  FROM ip_reg group by reg';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());
$query = 'SELECT b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE a.ip = b.ip group by b.reg';
$res2 = mysql_query($query) or die('Query failed: ' . mysql_error());
//name="active","inactive"
//echo $res1;
//echo $res2;
$tmp=array();
$tmp['name']='active';
$tmp['data']=array();
while($row = mysql_fetch_array($res2) ){

	array_push($tmp['data'],array($row['reg'],intval($row['count'])));
}
array_push($res,$tmp);
//echo json_encode($res);

$tmp=array();
$tmp['name']='inactive';
$tmp['data']=array();
$i=0;
while($row = mysql_fetch_array($res1) ){
	#echo $res[0]['data'][$i][1];
	array_push($tmp['data'],array($row['reg'],$row['total']-$res[0]['data'][$i][1]));
	$i=$i+1;
}
array_push($res,$tmp);
echo json_encode($res);
// Free resultset
mysql_free_result($res1);
mysql_free_result($res2);

}
if($type==1)
{
$from_mask=str_replace("-","",$from_mask);
$tb="scanner_".$from_mask;
$res=array();
$query = 'SELECT a.port as port,b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE a.ip = b.ip group by b.reg,a.port';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_array($res1) ){

	array_push($res,array($row['port'],$row['reg'],intval($row['count'])));
}
echo json_encode($res);

}
if($type==2)
{
$from_mask=str_replace("-","",$from_mask);
$tb="scanner_".$from_mask;
$res=array();

$query = 'SELECT b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE  a.code="0" and a.ip = b.ip group by b.reg';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($res1) ){

array_push($res,array("error",$row['reg'],intval($row['count'])));
}
$query = 'SELECT b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE  a.code=200 and a.ip = b.ip group by b.reg';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($res1) ){


array_push($res,array("200",$row['reg'],intval($row['count'])));
}
$query = 'SELECT b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE  a.code like "4%" and a.ip = b.ip group by b.reg';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($res1) ){

array_push($res,array("400",$row['reg'],intval($row['count'])));
}
$query = 'SELECT b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE  a.code like "5%" and a.ip = b.ip group by b.reg';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($res1) ){

array_push($res,array("500",$row['reg'],intval($row['count'])));
}
$query = 'SELECT b.reg as reg, count(*) as count FROM '.$tb.' a, ip_reg  b WHERE  a.code!="" and a.code != "0" and a.code != "200" and a.code not like "5%" and a.code not like "4%" and a.ip = b.ip group by b.reg';
$res1 = mysql_query($query) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($res1) ){

array_push($res,array("other",$row['reg'],intval($row['count'])));
}
echo json_encode($res);

}

if($type==3)
{
	$detail=$_GET["detail"];

$from_mask=str_replace("-","",$from_mask);
$tb="scanner_".$from_mask;
if($detail=="other")$query='SELECT code as c , count(*) as count FROM '.$tb.' WHERE code!=0 and code!="" and code!="200" and code not like "5%" and code not like "4%" group by code' ;
if($detail=="500")$query='SELECT code as c, count(*) as count FROM '.$tb.' WHERE code like "5%" group by code' ;
if($detail=="400")$query='SELECT code as c, count(*) as count FROM '.$tb.' WHERE code like "4%" group by code' ;
if($detail=="200")$query='SELECT code as c, count(*) as count FROM '.$tb.' WHERE code = 200 group by code' ;
if($detail=="error")$query='SELECT content as c , count(*) as count FROM '.$tb.' WHERE code = "0" group by content' ;
//echo $query;
$res=array();
$res1 = mysql_query($query) or die('Query failed1: ' . mysql_error());
while($row = mysql_fetch_array($res1) ){
	$c=$row["c"];
	$c=str_replace("(", "", $c);
	$c=str_replace("'", "", $c);
	$c=str_replace('"', "", $c);
	
	$pos = strpos($c, "Exceeded");
	if($pos!==false){
			$c="Exceeded30Redirctions";
	}
	$pos = strpos($c, "SSL");
	if($pos!==false){
			$c="SSLError";
	}

	array_push($res,array($c,"null",intval($row['count'])));
}
echo json_encode($res);

}

mysql_close($db);
?>
